/**
 * 
 */
package edu.bbk.muc.lifetracker.logging;

import android.database.Cursor;
import edu.bbk.muc.lifetracker.DbAdapter;
import edu.bbk.muc.lifetracker.common.Acceleration;
import edu.bbk.muc.lifetracker.common.Event;
import edu.bbk.muc.lifetracker.common.SimpleLocation;

/**
 * This class is responsible for creating text log of events.
 * 
 * @author irenatyler and romankirillov
 * 
 */
public class TextLogger extends Logger {

  /**
   * Initialises this {@link TextLogger}'s and parent class fields.
   * @param dbAdapter DB adapter which is used to read stored data.
   */
  public TextLogger(DbAdapter dbAdapter) {
    super(dbAdapter);
    fileExtension = ".txt";
    type = "Text";
  }

  @Override
  public
  String getFileContent() {
    return getText();
  }

  
  /**
   * Gets text for the whole file.
   * @return text for the whole file.
   */
  private String getText() {
    Cursor c = dbAdapter.fetchAllEvents();
    String text = "";
    int eventNo = 0; // event number
    Event curEvent; // current event
    // for each logged event create Placemark tag
    c.moveToFirst();
    while (!c.isAfterLast()) {
      eventNo++;
      curEvent = Event.fromJSON(c.getString(1));
      text = text
          + String.format("Timestamp: %1$s; %2$s; %3$s; %4$s;",
              getTimeStamp(curEvent), getLocationText(curEvent), getLightText(curEvent), 
              getAccelerationText(curEvent))
          + '\r';
      c.moveToNext();
    }
    c.close();
    return text;
  }

  /**
   * Gets text with event's acceleration information.
   * @param event for which to get acceleration information.
   * @return event's acceleration information.
   */
  private String getAccelerationText(Event event) {
    Acceleration acceleration = event.getAcceleration();
    String accelLateral = acceleration.getXAcceleration().toString();
    String accelLongitudinal = acceleration.getYAcceleration().toString();
    String accelVertical = acceleration.getZAcceleration().toString();
    return String.format("Acceleration: lateral - %1$s, longitudinal - %2$s, vertical - %3$s",
        accelLateral, accelLongitudinal, accelVertical);
  }

  /**
   * Gets text with event's light sensor information.
   * @param event for which to get light sensor information.
   * @return light sensor information.
   */
  private String getLightText(Event event) {
    return String.format("Light: %1$s",event.getAmbientLight());
  }

  /**
   * Gets text with event's location information.
   * @param event for which to get location information.
   * @return event's location information.
   */
  private String getLocationText(Event event) {
    SimpleLocation location = event.getLocation();
    return String.format("Location: longitude - %1$s, latitude - %2$s, altitude - %3$s",
        location.getLon(), location.getLat(), location.getAlt());
  }

  @Override
  public String getType() {
    return type;
  }

  @Override
  public String getFileExtension() {
    return fileExtension;
  }

}
